import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: AnimatedModalBarrierExample());
  }
}

class AnimatedModalBarrierExample extends StatefulWidget {
  @override
  _AnimatedModalBarrierExampleState createState() =>
      _AnimatedModalBarrierExampleState();
}

class _AnimatedModalBarrierExampleState
    extends State<AnimatedModalBarrierExample>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<Color?> _animation;
  @override
  void initState() {
    super.initState();
    //动画控制器
    _animationController = AnimationController(
      vsync: this,
      duration: Duration(seconds: 2),
    );

    //创建动画
    _animation = ColorTween(
      begin: Colors.blue.withValues(alpha: 0.5),
      end: Colors.green.withValues(alpha: 0.5),
    ).animate(_animationController);

    //
    _animationController.repeat(reverse: true);
  }

  @override
  void dispose() {
    super.dispose();
    _animationController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('AnimatedModalBarrier 示例')),
      body: Stack(
        children: [
          Image.asset('asset/images/s01.png'),
          AnimatedModalBarrier(
            color: _animation,
            dismissible: true,
            onDismiss: () {
              _animationController.stop();
            },
          ),
        ],
      ),
    );
  }
}
